package com.gmgx.hrms.dao.impl;

import com.gmgx.hrms.dao.EmpDao;
import com.gmgx.hrms.dao.OLDao;
import com.gmgx.hrms.entity.Emp;
import com.gmgx.hrms.entity.OL;
import com.gmgx.hrms.util.JDBCTools;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OLDaoImpl implements OLDao {
    private JDBCTools tools=new JDBCTools();

    @Override
    public boolean queryOLByEmpidAndTimeRangeForLeave(OL ol) {
        boolean flag=false;
        String sql="select * from t_ol where ol_emp=? and (date(ol_start)=date(?) or date(ol_end)=date(?)) and ol_status!='6' and ol_type='请假'";
        ResultSet rs= tools.query(sql,ol.getOl_emp().getEmp_id(),ol.getOl_start(),ol.getOl_end());
        try {
            if(rs.next()){
                flag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return flag;
    }

    @Override
    public boolean addOLForLeave(OL ol) {
       int i= tools.update("insert into t_ol (ol_emp,ol_start,ol_end,ol_status,ol_type,ol_reason) values(?,?,?,?,?,?)",ol.getOl_emp().getEmp_id(),
                ol.getOl_start(),ol.getOl_end(),ol.getOl_status(),ol.getOl_type(),ol.getOl_reason());
        return i>0;
    }

    @Override
    public List<OL> queryOLByEmpidForLeave(int empid) {
        List<OL> list=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_ol where ol_emp=? and ol_type='请假'",empid);
        try {
            while(rs.next()){
                    OL ol=new OL();
                    ol.setOl_id(rs.getInt("ol_id"));
                    ol.setOl_reason(rs.getString("ol_reason"));
                    ol.setOl_status(rs.getString("ol_status"));
                    ol.setOl_type(rs.getString("ol_type"));
                    ol.setOl_end(rs.getTimestamp("ol_end").toLocalDateTime());
                    ol.setOl_start(rs.getTimestamp("ol_start").toLocalDateTime());
                    EmpDao dao=new EmpDaoImpl();
                    Emp emp=   dao.queryEmpById(rs.getInt("ol_emp"));
                    ol.setOl_emp(emp);
                   list.add(ol);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;
    }

    @Override
    public List<OL> queryOLByDeptIdForLeave(int deptid) {
        List<OL> list=new ArrayList<>();
        ResultSet rs=tools.query("select  *  from t_ol inner  join   t_emp on ol_emp=emp_id where emp_dept=? and ol_type='请假' and ol_status='0'",deptid);
        try {
            while(rs.next()){
                OL ol=new OL();
                ol.setOl_id(rs.getInt("ol_id"));
                ol.setOl_reason(rs.getString("ol_reason"));
                ol.setOl_status(rs.getString("ol_status"));
                ol.setOl_type(rs.getString("ol_type"));
                ol.setOl_end(rs.getTimestamp("ol_end").toLocalDateTime());
                ol.setOl_start(rs.getTimestamp("ol_start").toLocalDateTime());
                EmpDao dao=new EmpDaoImpl();
                Emp emp=   dao.queryEmpById(rs.getInt("ol_emp"));
                ol.setOl_emp(emp);
                list.add(ol);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;
    }

    @Override
    public boolean updateOLByOLidForLeave(int olid, String status) {
        return tools.update("update t_ol set ol_status=? where ol_id=?",status,olid)>0;
    }
}
